<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-13
 * Time: 下午6:06
 * To change this template use File | Settings | File Templates.
 */
class model_pay_99bill_gamecard extends MY_Model
{
    /**
     * 支付渠道配置
     * @var array
     */
    private $conf = array();

    public function __construct()
    {
        $this->conf['merchant_id'] = config_item('99bill_gamecard_merchant_id');   //商户号
        $this->conf['merchant_key'] = config_item('99bill_gamecard_merchant_key'); //密钥
        $this->conf['action_url'] = config_item('99bill_gamecard_request_url');    //支付接口地址
        $this->conf['InputCharset'] = 1;                        //字符集 1 utf-8
        $this->conf['PayType'] = '00';
        $this->conf['Language'] = 1;
        $this->conf['SignType'] = 1;                            //签名类型1 md5
        $this->conf['Version'] = 'v2.0';                        //块钱接口版本
        $this->conf['payback'] = config_item('pay_back_url'); //支付完毕回调地址
        $this->conf['redoFlag'] = '1';
        $this->conf['pid'] = '';
    }

    /******************************************请求****************************************/
    public function request(array $arr)
    {
        $input['merchantAcctId'] = $this->conf['merchant_id'];
        $input['orderId'] = $arr['order_sn'];
        $input['orderAmount'] = $arr['amount'];
        $input['payType'] = $arr['bank'];
        $input['pageUrl'] = $this->conf['payback'].'/view';
        $input['bgUrl'] = $this->conf['payback'];
        $input['ext1'] = '';
        $input['ext2'] = '';
        $input['signMsg'] = $this->sign($input);

        return $this->makeRequest($this->conf['action_url'], $input);
    }

    /******************************************响应****************************************/
    function response()
    {
        $input['payResult'] = $this->input->get_post('payResult');
        $input['dealId'] = $this->input->get_post('dealId');
        $input['merchantAcctId'] = $this->input->get_post('merchantAcctId');
        $input['orderId'] = $this->input->get_post('orderId');
        $input['payAmount'] = $this->input->get_post('payAmount');
        $input['cardNumber'] = $this->input->get_post('cardNumber');
        $input['ext1'] = $this->input->get_post('ext1');
        $input['ext2'] = $this->input->get_post('ext2');
        //$input['errInfo'] = $this->input->get_post('errInfo');

        $sign = $this->input->get_post('signMsg');
//echo '<pre>';print_r($_REQUEST);exit;
        $rData = array(
            'result' => $input['payResult'],
            'third_part_order_sn' => $input['dealId'],
            'amount' => $input['payAmount'],
            'order_sn' => $input['orderId'],
            'pay_user' => '',
            'pay_user_id' => '',
            'request_type' => $this->uri->segment(3) == 'view' ? 1 : 2,
            'pay_channel' => '',
            'bank_order_id' => $input['dealId'],
            'pay_type' => '99bill_card',
            'response' => "<result>1</result><redirecturl>{$this->conf['payback']}/view</redirecturl>",
        );

        $my_sign = $this->sign($input);
        //echo '<pre>';print_r($rData);exit;
        //状态值(status) 1 成功， 2 签名错误， 3 订单支付失败
        $rData['status'] = 1;
        if (strtoupper($sign) !== $my_sign) {
            $rData['status'] = 2;
        }
        //判断支付状态
        if ($input['payResult'] != '0') {
            $rData['status'] = 3;
        }
        return $rData;
    }

    private function makeRequest($api='' ,$data=array())
    {
        $form = "<form method=\"POST\" name=\"pay_form\" action=\"{$api}\">\n";
        foreach($data as $name=>$value)
        {
            $form .= "<input type=\"hidden\" name=\"{$name}\" value=\"{$value}\">\n";
        }
        $form .= "</form>\n";
        $form .= "<script>document.forms['pay_form'].submit()</script>";
        return $form;
    }

    private function sign($data)
    {
        $sign = '';
        foreach($data as $k=>$v)
        {
            $v!=='' && $sign .= "{$k}={$v}&";
        }
        $sign .= "key=".$this->conf['merchant_key'];
        return strtoupper(md5($sign));
    }
}